﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmGhiTra : DevExpress.XtraEditors.XtraForm
    {
        #region khai bao bien
        CT_PhieuYeuCau_BLL ct_pyc_bll = new CT_PhieuYeuCau_BLL();
        TextMessageBox box = new TextMessageBox();
        Image image;
        string maPhieuYC = ""; 
        string maDocGia = "";
        string soCaBiet = "";
        bool ktGhiTra = false;
        bool ktGiaHan = false;
        bool ktQuaHan = false;
        string ngayDuTra = "";

        #endregion


        #region LoadForm

        public frmGhiTra()
        {
            InitializeComponent();
        }

        private void frmGhiTra_Load(object sender, EventArgs e)
        {

        }

        #endregion

        #region cac chuc nang

        private void btnGhiTra_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            grv_Data.CloseEditor();
            grv_Data.UpdateCurrentRow();
            CT_PhieuYeuCau_Public ct_public = new CT_PhieuYeuCau_Public();
            //nếu chọn trong grid thì ghi trả với dữ liệu ở gridview
            checkRow();
            if (ktGhiTra == true)
            {
                for (int i = 0; i < grv_Data.RowCount; i++)
                {
                    if (Convert.ToBoolean(grv_Data.GetRowCellValue(i, colCheck)) == true)
                    {
                        //update 
                        ct_public.MaPhieuYC = grv_Data.GetRowCellValue(i, colMaPhieuYC).ToString();
                        ct_public.SoCaBiet = grv_Data.GetRowCellValue(i, colSoCaBiet).ToString();
                        ct_public.NgayTra = box.GetSystemDate();
                        ct_public.TrangThaiMuon = false;
                        ct_public.MaNhanVien1 = box.MaNhanVien;

                        ct_pyc_bll.Update_GhiTra(ct_public);

                    } 
                    XtraMessageBox.Show("Ghi Trả thành công!",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
               
            }
            else//ko thì ghi trả ở textbox
            {
                if (maPhieuYC != "" && txtSoCaBiet.Text.Trim() != "")
                {
                    //update 
                    ct_public.MaPhieuYC = maPhieuYC;
                    ct_public.SoCaBiet = txtSoCaBiet.Text.Trim();
                    ct_public.NgayTra = box.GetSystemDate();
                    ct_public.TrangThaiMuon = false;
                    ct_public.MaNhanVien1 = box.MaNhanVien;

                    ct_pyc_bll.Update_GhiTra(ct_public);
                    XtraMessageBox.Show("Ghi Trả thành công!", box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            GridView();
        }
        private void btnCheckAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            for (int i = 0; i < grv_Data.RowCount; i++)
                grv_Data.SetRowCellValue(i, colCheck, true);
        }

        private void btnUncheck_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            for (int i = 0; i < grv_Data.RowCount; i++)
                grv_Data.SetRowCellValue(i, colCheck, false);
        }

        private void btnGiaHan_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            grv_Data.CloseEditor();
            grv_Data.UpdateCurrentRow();
            CheckGiaHan();
            if (ktGiaHan == true)
            {
                Lay_Data_GiaHan();
                frmGiaHan frm = new frmGiaHan();
                frm.maPhieuYC = maPhieuYC;
                frm.soCabiet = soCaBiet;
                frm.maDocGia = maDocGia;
                frm.ngayDuTra = ngayDuTra;
                frm.ShowDialog();
            }
        }

        private void btnQuaHan_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            grv_Data.CloseEditor();
            grv_Data.UpdateCurrentRow();
            checkQuaHan();
            if (ktQuaHan == true)
            {
                soCaBiet = "";
                Lay_Data_QuaHan();
                frmPhieuPhat frm = new frmPhieuPhat();
                frm.maDocGia = maDocGia;
                frm.soCaBiet = soCaBiet;
                frm.ShowDialog();
            }
        }

        private void linkLichSuMuon_Click(object sender, EventArgs e)
        {
            frmLichSuMuon frm = new frmLichSuMuon();
            frm.maDocGia = maDocGia;
            frm.ShowDialog();
        }

        private void linhThongTinCT_Click(object sender, EventArgs e)
        {
            frmThongTinChiTiet frm = new frmThongTinChiTiet();
            frm.maDocGia = maDocGia;
            frm.ShowDialog();
        }
        #endregion

        #region ham viet them
        private void checkQuaHan()
        {
            for (int i = 0; i < grv_Data.RowCount; i++)
                if (Convert.ToBoolean(grv_Data.GetRowCellValue(i, colCheck)) == true && box.ToInt(grv_Data.GetRowCellValue(i, colQuaHan)) > 0)
                {
                    ktQuaHan = true;
                    break;
                }
        }
        private void Lay_Data_QuaHan()
        {
            for (int i = 0; i < grv_Data.RowCount; i++)
                if (Convert.ToBoolean(grv_Data.GetRowCellValue(i, colCheck)) == true && box.ToInt(grv_Data.GetRowCellValue(i, colQuaHan)) > 0)
                {
                    
                    soCaBiet += grv_Data.GetRowCellValue(i, colSoCaBiet).ToString() + ",";
                }
        }
        private void checkRow()
        {
            for (int i = 0; i < grv_Data.RowCount; i++)
            {
                if (Convert.ToBoolean(grv_Data.GetRowCellValue(i, colCheck)) == true)
                {
                    ktGhiTra = true;
                    break;
                }
            }
        }

        private void Load_Data()
        {
            string tuNgay="",denNgay="";
            DataTable dt_GiaoVien = ct_pyc_bll.Load_GhiTra_GiaoVien(txtSoCaBiet.Text.Trim());
            DataTable dt_HocVien = ct_pyc_bll.Load_GhiTra_HocVien(txtSoCaBiet.Text.Trim());
            if (dt_GiaoVien.Rows.Count > 0 && dt_HocVien.Rows.Count == 0)
            {
                try
                {
                    lblHoVaTen.Text = dt_GiaoVien.Rows[0]["HoVaTen"].ToString();
                    lblNgaySinh.Text = dt_GiaoVien.Rows[0]["NgaySinh"].ToString();
                    tuNgay = dt_GiaoVien.Rows[0]["NgayLamThe"].ToString();
                    denNgay = dt_GiaoVien.Rows[0]["NgayHetHan"].ToString();
                    lblGiaTriThe.Text = tuNgay.Substring(0, 9) + "  Đến Ngày:  " + denNgay.Substring(0, 9);
                    lblNhanDe.Text = dt_GiaoVien.Rows[0]["NhanDe"].ToString();
                    lblSoCaBiet.Text = dt_GiaoVien.Rows[0]["MaTaiLieu"].ToString();
                    lblTacGiaChinh.Text = dt_GiaoVien.Rows[0]["TacGiaChinh"].ToString();
                    pBAnh.Image = ConvertToImage(dt_GiaoVien);
                    maDocGia=dt_GiaoVien.Rows[0]["MaDocGia"].ToString();
                    lblLoaiThe.Text=dt_GiaoVien.Rows[0]["TenLoaiThe"].ToString();
                    maPhieuYC = dt_GiaoVien.Rows[0]["MaPhieuYC"].ToString();
                    lblMaBanDoc.Text = maDocGia;
                }
                catch (Exception)
                { }
            }
            if (dt_GiaoVien.Rows.Count == 0 && dt_HocVien.Rows.Count > 0)
            {
                try
                {
                    lblHoVaTen.Text = dt_HocVien.Rows[0]["HoVaTen"].ToString();
                    lblNgaySinh.Text = dt_HocVien.Rows[0]["NgaySinh"].ToString();
                    tuNgay = dt_HocVien.Rows[0]["NgayLamThe"].ToString();
                    denNgay = dt_HocVien.Rows[0]["NgayHetHan"].ToString();
                    lblGiaTriThe.Text = tuNgay.Substring(0, 9) + "Đến Ngày:  " + denNgay.Substring(0, 9);
                    lblNhanDe.Text = dt_HocVien.Rows[0]["NhanDe"].ToString();
                    lblSoCaBiet.Text = dt_HocVien.Rows[0]["MaTaiLieu"].ToString();
                    lblTacGiaChinh.Text = dt_HocVien.Rows[0]["TacGiaChinh"].ToString();
                    pBAnh.Image = ConvertToImage(dt_HocVien);
                    maDocGia = dt_HocVien.Rows[0]["MaDocGia"].ToString();
                    lblLoaiThe.Text = dt_HocVien.Rows[0]["TenLoaiThe"].ToString();
                    maPhieuYC = dt_HocVien.Rows[0]["MaPhieuYC"].ToString();
                    lblMaBanDoc.Text = maDocGia;
                }
                catch (Exception)
                { }
            }
            else if (dt_GiaoVien.Rows.Count == 0 && dt_HocVien.Rows.Count == 0)
                XtraMessageBox.Show("Không tồn tại Mã độc giả này hoặc Mã độc giả đang trong trạng thái không hoạt động",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }
        private void GridView()
        {
            grd_Data.DataSource = ct_pyc_bll.Load_TaiLieu_GhiTra(maDocGia);
        }
        private Image ConvertToImage(DataTable d)
        {
            try
            {

                byte[] stream = (byte[])d.Rows[0]["Anh"];

                using (MemoryStream ms = new MemoryStream(stream, 0, stream.Length))
                {
                    ms.Write(stream, 0, stream.Length);
                    image = Image.FromStream(ms, true);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return image;
        }

        private void Lay_Data_GiaHan()
        {
            maPhieuYC = "";
                for (int i = 0; i < grv_Data.RowCount; i++)
                {
                    if (Convert.ToBoolean(grv_Data.GetRowCellValue(i, colCheck)) == true && grv_Data.GetRowCellValue(i, colTrangThaiGiaHan).ToString().Equals("Chưa Gia Hạn"))
                    {
                        maPhieuYC += grv_Data.GetRowCellValue(i, colMaPhieuYC).ToString() + ",";
                        soCaBiet += grv_Data.GetRowCellValue(i, colSoCaBiet).ToString() + ",";
                        ngayDuTra += grv_Data.GetRowCellValue(i, colNgayDuTra).ToString() + ",";
                    }
                }
            
        }

        private void CheckGiaHan()
        {
            for (int i = 0; i < grv_Data.RowCount; i++)
            {
                if (Convert.ToBoolean(grv_Data.GetRowCellValue(i, colCheck)) == true && grv_Data.GetRowCellValue(i, colTrangThaiGiaHan).ToString().Equals("Chưa Gia Hạn") && Convert.ToInt32(grv_Data.GetRowCellValue(i, colQuaHan)) == 0)
                {
                    ktGiaHan = true;
                    break;
                }
            }
        }
        #endregion

        private void txtSoCaBiet_Leave(object sender, EventArgs e)
        {
            Load_Data();
            GridView();
        }

        private void grv_Data_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            //if ((e.RowHandle != -1) && Convert.ToInt32( grv_Data.GetRowCellValue(e.RowHandle, colQuaHan))>0)
            //{
            //    try
            //    {
            //        e.Appearance.BackColor = System.Drawing.Color.Red;
            //    }
            //    catch { }
            //}
        }

        
    }
}